import { fromJS } from 'immutable';
import { Utils } from 'kryfe-lib';
import * as actionTypes from '../consts/actionTypes';

const { createReducer, customReducerUtil } = Utils;
const { initCustomState, customResultParser } = customReducerUtil;

const initialState = fromJS({
  async: null,
  customAsync: initCustomState(),
  sync: null
});

const handlers = {
  [actionTypes.ASYNC_PROMISE_ACTION](state, { payload }) {
    return state.set('async', fromJS(payload.body));
  },
  
  [actionTypes.ASYNC_CUSTOM_ACTION](state, { payload, error, meta }) {
    return customResultParser(state, { payload, error, meta });
  },
  
  [actionTypes.SYNC_ACTION](state, { payload }) {
    return state.set('sync', fromJS(payload));
  }
};

export default createReducer(initialState, handlers);
